﻿using System;
using System.Collections.Generic;
using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Client.View
{
    /// <summary>
    /// Interaction logic for ComicListView.xaml
    /// </summary>
    public partial class ComicListView
    {
        private readonly ComicController _comicControl;

        private readonly Member _member;
        private IList<Comic> _comics;
        private IList<ComicData> _comicDatas;

        public ComicListView(Member member)
        {
            InitializeComponent();

            _comicControl = new ComicController();

            _member = member;
            labelUsername.Content = _member.Username;

            _comics = _comicControl.GetAllComic();
            _comicDatas = _comicControl.GetAllComicData();

            dataGridComic.ItemsSource = _comicDatas;
        }

        private void RefreshGrid()
        {
            dataGridComic.ItemsSource = null;

            _comics = _comicControl.GetAllComic();
            _comicDatas = _comicControl.GetAllComicData();

            dataGridComic.ItemsSource = _comicDatas;
        }

        private void ButtonDetailClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridComic.SelectedItem == null)
                    throw new NullReferenceException();

                var newWindow = new ComicDetailView(_member, _comics[dataGridComic.SelectedIndex]);

                if (newWindow.ShowDialog() != false)
                {
                }
                else
                {
                    RefreshGrid();
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("pilih yang mau dilihat");
            }
        }
    }
}
